<?php

namespace app\common\validate;

class Base extends \think\Validate {

	protected $create_require=[];
	protected $update_require=[];

	/**
	 * Check id validity
	 * @param  array $val the data will be checked
	 * @param  string $modelname name of the model you want to use
	 * @param  mixed $data the data of whole form data
	 * @param  string $field the name of the field is validating
	 * @param  string $field the customer reading name of the field is validating
	 * @return true|string
	 */
	public function check_id($val,$modelname,$data,$field,$field_detial){
		if ($model=model($modelname)){
			if ($model->get($val)){
				return true;
			} else {
				return "没有找到依赖资源(".$field_detial.")";
			}
		} else {
			return "后端又出小差错了，需要指定模型，却没有传入（快去提醒他）";
		}
	}

	public function check_id0($val,$modelname,$data,$field,$field_detial){
		if ($model=model($modelname)){
			if ($val==0 || $model->get($val)){
				return true;
			} else {
				return "没有找到依赖资源(".$field_detial.")";
			}
		} else {
			return "后端又出小差错了，需要指定模型，却没有传入（快去提醒他）";
		}
	}

	public function check_status($val,$modelname,$data,$field,$field_detial) {
		if ($val==0||$val==1)
			return true;
		else
			return $field_detial."只能填0或1";
	}


	public function sceneCreate(){
		$tmp=$this;
		foreach ($tmp->create_require as $require){
			$tmp->append($require,'require');
		}
		return $tmp;
	}
	public function sceneUpdate(){
		$tmp=$this;
		foreach ($tmp->update_require as $require){
			$tmp->append($require,'require');
		}
		return $tmp;
	}
}



?>